package com.wanglibao.woodtrade.woodtrade.ui.activity.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wanglibao.woodtrade.woodtrade.R;
import com.wanglibao.woodtrade.woodtrade.entity.GoodsListDetails;
import com.wanglibao.woodtrade.woodtrade.ui.activity.EditCommodityActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;


public class ItemLvMyCommodityTwoActivityAdapter extends BaseAdapter {

    private List<GoodsListDetails.ListBean> mList = new ArrayList<>();

    private Context context;
    private LayoutInflater layoutInflater;

    public ItemLvMyCommodityTwoActivityAdapter(Context context,List<GoodsListDetails.ListBean> list) {
        this.context = context;
        this.mList=list;
        this.layoutInflater = LayoutInflater.from(context);
    }

    public void setList(List<GoodsListDetails.ListBean> list) {
        mList = list;
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public GoodsListDetails.ListBean getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.item_lv_my_commodity_two_activity, null);
            convertView.setTag(new ViewHolder(convertView));
        }
        initializeViews((GoodsListDetails.ListBean) getItem(position), (ViewHolder) convertView.getTag());
        return convertView;
    }

    private void initializeViews(final GoodsListDetails.ListBean object, ViewHolder holder) {
        //TODO implement
        holder.tvItemLvMyCommodityTwoClass.setText(object.getTitle());
        holder.tvItemLvMyCommodityTwoKcl.setText(object.getUp_quantity()+object.getUnit_name());
        holder.tvItemLvMyCommodityTwoYsj.setText(object.getPrice()+"元");
        holder.tvItemLvMyCommodityTwoDj.setText(object.getRank());
        holder.tvItemLvMyCommodityTwoOption.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(context,EditCommodityActivity.class);
                intent.putExtra("goods_id",object.getGoods_id());
                context.startActivity(intent);
            }
        });
    }

    static class ViewHolder {
        @Bind(R.id.tv_item_lv_my_commodity_two_class)
        TextView tvItemLvMyCommodityTwoClass;
        @Bind(R.id.tv_item_lv_my_commodity_two_kcl)
        TextView tvItemLvMyCommodityTwoKcl;
        @Bind(R.id.tv_item_lv_my_commodity_two_ysj)
        TextView tvItemLvMyCommodityTwoYsj;
        @Bind(R.id.tv_item_lv_my_commodity_two_dj)
        TextView tvItemLvMyCommodityTwoDj;
        @Bind(R.id.tv_item_lv_my_commodity_two_option)
        LinearLayout tvItemLvMyCommodityTwoOption;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
